//
//  ViewController.m
//  Reader
//
//  Created by mac on 16/12/6.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "MYCollectionViewCell.h"
#import "BooKViewController.h"
#import "MenuView.h"

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSMutableArray <UIImage *> *images;
    NSMutableArray <NSString *> *imageName;
   
}

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *layout;


@end

@implementation ViewController

static NSString * const identifier = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadImageData];
  
    
    self.title = @"Reader";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"背景-2"] forBarMetrics: UIBarMetricsDefault];
    
    
  
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.layout];
    
    [self.collectionView registerClass:[MYCollectionViewCell class] forCellWithReuseIdentifier:identifier];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    
    self.collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"背景-1"]];
    self.layout.minimumLineSpacing = 50;
    self.layout.minimumInteritemSpacing = 20;
    //self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.itemSize = CGSizeMake((self.view.frame.size.width - 40 - 40) / 3, (self.view.frame.size.height - 64 - 100 - 100) / 3);
   
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongGesture:)];
    [self.collectionView addGestureRecognizer:longGesture];
    
    [self.view addSubview:self.collectionView];
    
    UIButton *add = [UIButton buttonWithType:UIButtonTypeCustom];
    add.frame = CGRectMake(30, self.view.frame.size.height - 64 - 80, 50, 50);
    [add setImage:[UIImage imageNamed:@"加号"] forState:UIControlStateNormal];
    add.showsTouchWhenHighlighted = YES;
    [self.view addSubview:add];
    
    [MenuView createMenuViewWithFrame:CGRectMake(50, 150, 80, 100) target:self];
    
    
    
    NSLog(@"%f",self.view.frame.size.height);
    
}

- (void)onLongGesture:(UILongPressGestureRecognizer *)longGesture
{
    switch(longGesture.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            //[longGesture locationInview:self.collectionView]获取当前长按位置的point
            //如果当前长按的位置不在item内[self.collectionView indexPathForItemPoint:]返回的index为空
            NSIndexPath *indexpath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
            
            NSLog(@"x:%3.0f,y:%3.0f,index:%ld",[longGesture locationInView:self.collectionView].x,[longGesture locationInView:self.collectionView].y,indexpath.item
                  );
            
            if(indexpath == nil)
            {
                break;
            }
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexpath];
        } break;
         
        case UIGestureRecognizerStateChanged:
        {
            //长按选中item后开始移动时longGesture.state为UIGestureRecognizerStateChanged
            //[self.collectionView updateInteractiveMovementTargetPosition:]更新item当前移动到的位置
            [self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
            NSLog(@"x2:%f,y2:%f",[longGesture locationInView:self.collectionView].x,[longGesture locationInView:self.collectionView].y);
        } break;
            
        case UIGestureRecognizerStateEnded:
        {
            [self.collectionView endInteractiveMovement];
        } break;
    }
    

}

- (void)loadImageData
{
    
    if(!images)
    {
        images = [NSMutableArray new];
    }
    if(!imageName)
    {
        imageName = [NSMutableArray new];
    }
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSArray <NSString *> *arr = [bundle pathsForResourcesOfType:@"jpeg" inDirectory:nil];
   
    for(NSString *str in arr)
    {
        NSString *name = [str componentsSeparatedByString:@"/"].lastObject;
        [imageName addObject:name];
    }
    NSLog(@"%@",imageName);
    for(NSString *name in imageName)
    {
     
        UIImage *image = [UIImage imageNamed:name];
        [images addObject:image];
    }
    NSLog(@"数量：%ld",images.count);
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imgView.image = images[indexPath.row];
    return cell;
}

//设置section边距，此项目只有一个section
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(50, 20, 100, 20);
}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    id obj = [images objectAtIndex:sourceIndexPath.item];
    [images removeObject:obj];
    [images insertObject:obj atIndex:destinationIndexPath.item];
    
    obj = [imageName objectAtIndex:sourceIndexPath.item];
    [imageName removeObject:obj];
    [imageName insertObject:obj atIndex:destinationIndexPath.item];
    NSLog(@"%@",imageName);
    [self.collectionView reloadData];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BooKViewController *bookVC = [BooKViewController new];
    bookVC.bookName = imageName[indexPath.item];
    bookVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    // UIModalTransitionStyleCoverVertical
    //UIModalTransitionStyleFlipHorizontal
    //UIModalTransitionStyleCrossDissolve
    //UIModalTransitionStylePartialCurl
    //[self.navigationController pushViewController:bookVC animated:YES];
    [self presentViewController:bookVC animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
